<template>
    <d2-container>
        <el-table stripe
                  ref="table"
                  :data="tableData"
                  max-height="600"
                  class="amend-talbe"
                  v-loading="loadList"
                  :span-method="listSpanMthod"
                  :cell-style="listCellStyle"
                  element-loading-text="数据加载中,请稍候"
                  element-loading-spinner="el-icon-loading">
            <el-table-column show-overflow-tooltip
                             v-for="(item, index) in columns"
                             :sortable="item.sortable ? item.sortable : false"
                             :width="item.width"
                             :key="index"
                             :prop="`${item.key}`"
                             :label="item.title">
            </el-table-column>
            <el-table-column label="操作"
                             width='150px'
                             align='center'>
                <template slot-scope="scope">
                    <el-link size='mini'
                             type='primary'
                             :underline='false'
                             @click="viewInfo(scope)">详情</el-link>
                </template>
            </el-table-column>
        </el-table>
    </d2-container>
</template>
<script>
export default {
    data () {
        return {
            tableData: [
                {
                    commonTarget: "13520154687",
                    childList: [
                        {
                            name: "话单-黄书杰",
                            personId: "207",
                            account: "13096845751",
                            callDuration: "567",
                            callCount: "2"
                        },
                        {
                            name: "话单-李如云",
                            personId: "208",
                            account: "13405667890",
                            callDuration: "4561",
                            callCount: "6"
                        }
                    ]
                },
                {
                    commonTarget: "13564851359",
                    childList: [
                        {
                            name: "话单-黄书杰",
                            personId: "207",
                            account: "13096845751",
                            callDuration: "875",
                            callCount: "2"
                        },
                        {
                            name: "话单-李如云",
                            personId: "208",
                            account: "13405667890",
                            callDuration: "2288",
                            callCount: "6"
                        }
                    ]
                },
                {
                    commonTarget: "13830168429",
                    childList: [
                        {
                            name: "话单-黄书杰",
                            personId: "207",
                            account: "13096845751",
                            callDuration: "875",
                            callCount: "2"
                        },
                        {
                            name: "话单-李如云",
                            personId: "208",
                            account: "13405667890",
                            callDuration: "2288",
                            callCount: "6"
                        }
                    ]
                },
                {
                    commonTarget: "15834982546",
                    childList: [
                        {
                            name: "话单-黄书杰",
                            personId: "207",
                            account: "13096845751",
                            callDuration: "0",
                            callCount: "1"
                        },
                        {
                            name: "话单-李如云",
                            personId: "208",
                            account: "13405667890",
                            callDuration: "933",
                            callCount: "3"
                        }
                    ]
                },
                {
                    commonTarget: "15892541467",
                    childList: [
                        {
                            name: "话单-黄书杰",
                            personId: "207",
                            account: "13096845751",
                            callDuration: "390",
                            callCount: "1",
                            childList: [
                                {
                                    name: "话单-李如云",
                                    personId: "208",
                                    account: "13405667890",
                                    callDuration: "1984",
                                    callCount: "6"
                                },
                                {
                                    name: "话单-李如云",
                                    personId: "208",
                                    account: "13405667890",
                                    callDuration: "1984",
                                    callCount: "6"
                                },
                            ]
                        },
                        {
                            name: "话单-李如云",
                            personId: "208",
                            account: "13405667890",
                            callDuration: "1984",
                            callCount: "6"
                        }
                    ]
                }
            ],
            columns: [
                {
                    title: "共同联系号码",
                    key: "commonTarget"
                },
                {
                    title: "分析对象名称",
                    key: "name",
                },
                {
                    title: "电话号码",
                    key: "account",
                },
                {
                    title: "通话时长",
                    key: "callDuration",
                },
                {
                    title: "通话次数",
                    key: "callCount",
                },
            ],
            loadList: false
        }
    },
    methods: {
        listSpanMthod ({ row, column, rowIndex, columnIndex }) {
            if (columnIndex < 1) {
                if (row.span_row && row.span_row > 0) {
                    return {
                        rowspan: row.span_row,
                        colspan: 1
                    };
                } else {
                    return {
                        rowspan: 0,
                        colspan: 0
                    };
                }
            }
        },
        listCellStyle ({ row, column, rowIndex, columnIndex }) {
            if (columnIndex < 1) {
                return {
                    backgroundColor: "transparent",
                }
            }
        },
        viewInfo (row) {

        },
        /* 处理数字 合并 */
        disposeTableData (data, fieldsStr = 'childList') {
            let tableData = [];
            data.length && data.forEach((item, index) => {
                let itemData = {}
                for (let key in item) {
                    key != fieldsStr ? itemData[key] = item[key] : ''; //数组子级
                }
                item[fieldsStr] && item[fieldsStr].forEach((t, i) => {
                    itemData.span_row = i == 0 ? item[fieldsStr].length : 0; //合并关键字段 合并几列
                    tableData.push({
                        ...itemData,
                        ...t,
                    });
                });

            });
            return tableData;
        },
    },
    mounted() {
        this.tableData = this.disposeTableData(this.tableData)
    }
}
</script>